﻿//========================================
// All rights reserved (C) 2017-2100
// CLR 版本：	4.0.30319.42000
// 命名空间:	SFJDService.SocketHandle
// 文 件 名：	PH06Update
// 作    者：	WooG
// 联系方式：	wooglle@163.com
// 创建时间：	2017/10/3 17:41:30
//========================================
using Network.UpdateData;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;

namespace SFJDService.SocketHandle
{
    /// <summary>
    /// 接收到升级指令
    /// </summary>
    class PH06Update : PacketHandle
    {
        public PH06Update(Socket socket, Packet packet) : base(socket, packet)
        {
        }

        protected override void Handle()
        {
            Version fileVersion = null;
            FileInfo fileInfo = null;
            try
            {
                fileInfo = new FileInfo(Runtime.pathUpdate + "\\SFJDService.exe");
            }
            catch { }
            if (fileInfo != null && fileInfo.Exists)//是则用本地文件升级
            {
                fileVersion = new Version(FileVersionInfo.GetVersionInfo(Runtime.pathUpdate + "\\SFJDService.exe").FileVersion);
                if (Runtime.version < fileVersion)
                {
                    //Process.Start(Runtime.pathUpdate + "\\Update.exe");
                    CopyFiles(Runtime.programPath, Runtime.pathTemp + "SFJD\\");
                    Delay(500);
                    using (Process process = new Process())
                    {
                        try
                        {
                            process.StartInfo.FileName = Runtime.pathTemp + "SFJD\\Update.exe";
                            process.Start();
                        }
                        catch { }
                    }
                }
            }
            else//否则网络更新
            {
                Launcher.SendMsg2All(typeof(PH06GetVersion).GetDictianaryKey(), Runtime.version.ToString());
                Delay(1000 * 10);
                if (Runtime.UsersOnLine.activeUsers.Count > 1)
                {
                    ClassActive active = Runtime.UsersOnLine.activeUsers[0];
                    active.Clone(Runtime.UsersOnLine.activeUsers[0]);
                    foreach (ClassActive user in Runtime.UsersOnLine.activeUsers)
                    {
                        if (user.Version > active.Version)
                        {
                            active.Clone(user);
                        }
                    }
                    if(active.Ip != Runtime.myIP)
                    {
                        Launcher.SendMsg2One(typeof(PH06GetUpdateFiles).GetDictianaryKey(), active.Guid, Runtime.version.ToString());
                    }
                }
            }
        }

        private void CopyFiles(string srcPath, string destPath)
        {
            try
            {
                File.Copy(srcPath + "\\Update.exe", destPath, true);
                File.Copy(srcPath + "Update.exe.config", destPath, true);
                File.Copy(srcPath + "\\Update.pdb", destPath, true);
            }
            catch { }
        }
    }
}
